// pages/releaseCaigou/releaseCaigou.js
var webContext = getApp().globalData.webContext;
var phoneNo = '';
var countdown = 60;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    productCatList: [],
    captcha: '获取验证码',
    flag: false,
    showDate:false,
    date:'截止时间'
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  loadproCat: function () {
    var _this = this
    wx.request({
      url: webContext + '/ebridge_h5/caigou/proCategory',//json数据地址
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        _this.setData({
          productCatList: res.data.data.pro_classify
        })
      }
    })
  },
  mianyi:function(){
    this.setData({
      quantity: "面议"
    })
  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    if (e.detail.value=='0'){
      this.setData({
        showDate: true
      })
    }else{
      this.setData({
        showDate: false
      })
    }
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  getPhoneNo: function (e) {
    console.log('手机号为：', e.detail.value)
    phoneNo = e.detail.value;
  },
  getCaptcha: function () {
    if (phoneNo) {
      var _this = this
      wx.request({
        url: webContext + '/ebridge_h5/caigou/getCaptcha?phoneNo=' + phoneNo,
        headers: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
        }
      })
      _this.settime();
    } else {
      wx.showModal({
        title: '提示',
        content: '请输入手机号码',
        showCancel: false
      })
    } 
  },

  settime: function () {
    var _this = this

    //发送验证码倒计时 
    if (countdown == 0) {
      _this.setData({
        captcha: '获取验证码',
        flag: false
      })
      countdown = 60;
      return;
    } else {
      _this.setData({
        captcha: "重新发送(" + countdown + ")",
        flag: true
      })
      countdown--;
    }
    setTimeout(function () {
      _this.settime();
    }
      , 1000)
  }, 
  formSubmit: function (e) {
   
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    var error=false;
    if (e.detail.value.proCat==''){
      error =true;
    }
    if (e.detail.value.title == '') {
      error = true;
    }
    if (e.detail.value.quantity == '') {
      error = true;
    }
   
    if (e.detail.value.effective =='' || (e.detail.value.effective == '0' && e.detail.value.deadline == '截止时间')){
      error = true;
    }
    if (e.detail.value.contactMan == '') {
      error = true; 
    }
    if (e.detail.value.mobile == '') {
      error = true;
    }
    if (error){
      wx.showModal({
        title: '提示',
        content: "请完善采购信息",
        showCancel: false
      })
      return;
    }
   
    var _this = this
    wx.request({
      url: webContext + '/ebridge_h5/caigou/releaseCaigou',//json数据地址
      data: e.detail.value,
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.data.collection=='1'){
          wx.reLaunch({
            url: '../../pages/caigouList/caigouList?refresh=1'
          })
        } else{
          wx.showModal({
            title: '提示',
            content: '发布失败',
            showCancel: false
          })
        }  
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadproCat();
    var util = require('../../utils/util.js')
    var timestamp = util.formatTime2(new Date)
    this.setData({
      curDate: timestamp,
      openId: getApp().globalData.openid
    })
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})